package com.ztgf.file.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.file.models.bo.FileInfoBO;
import com.ztgf.file.models.dto.FileInfoDTO;
import com.ztgf.file.models.service.IFileInfoService;
import com.ztgf.file.models.vo.FileInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 文件信息表控制器
 *
 * @author mxli
 * @Date 2020-07-10 14:19:43
 */

@RestController
@RequestMapping("/cms/fileInfo")
@Slf4j
@Api(value = "FileInfoController",tags = "文件信息表接口")
public class FileInfoController {

    @DubboReference
    private IFileInfoService fileInfoService;


    @ApiOperation(value = "删除文件信息表",notes = "删除文件信息表")
    @ApiImplicitParam(name = "id",value = "文件信息表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = fileInfoService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除文件信息表",notes = "批量删除文件信息表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        fileInfoService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<FileInfoDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        FileInfoDTO dto = fileInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "分页统计用户文件库使用大小",notes = "分页统计用户文件库使用大小")
    @PostMapping("/queryChildUserPageList")
    public Result<Page<FileInfoVO>> queryChildUserPageList(Long fileUserId, Integer type, int pageNo, int pageSize){
        Page<FileInfoVO> page = fileInfoService.queryChildUserPageList(fileUserId, type, pageNo, pageSize);

        return new Result<>(page);
    }

    @ApiOperation(value = "统计用户文件库使用大小",notes = "统计用户文件库使用大小")
    @PostMapping("/queryChildUserList")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileUserId",value = "平台id",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "type",value = "类型 0图片、1文件",required = true,paramType = "query", dataType = "Integer")
            ,@ApiImplicitParam(name = "childUserIds",value = "店铺id，以逗号【,】分隔",required = true,paramType = "query", dataType = "String")
    })
    public Result<List<FileInfoVO>> queryChildUserList(Long fileUserId, Integer type, String childUserIds){

        if(fileUserId == null || type == null || StrUtils.isEmpty(childUserIds)){
            return new Result<>(CodeEnum.FAIL_100002 );
        }


        String[] ids = childUserIds.split(",");
        if(ids == null || ids.length <= 0){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        List<Long> idList = new ArrayList<>();
        for (String id : ids) {
            try{
                idList.add(Long.parseLong(id));
            } catch (Exception e){
                log.error("childUserIds is error! ", e);
                return new Result<>(CodeEnum.FAIL_100002 );
            }
        }

        List<FileInfoVO> list = fileInfoService.queryChildUserList(fileUserId, type, idList);

        return new Result<>(list);
    }

    @ApiOperation(value = "文件信息表列表",notes = "文件信息表列表")
    @PostMapping("/queryPageList")
    public Result<Page<FileInfoDTO>> queryPageList(FileInfoBO bo){
        return selectPageList(bo);
    }

    @ApiOperation(value = "查询fileUser所有的数据",notes = "查询fileUser所有的数据")
    @PostMapping("/queryPageListByFileUser")
    public Result<Page<FileInfoDTO>> queryPageListByFileUser(FileInfoBO bo){
        return selectPageList(bo);
    }

    private Result<Page<FileInfoDTO>> selectPageList(FileInfoBO bo){
        Wrapper<FileInfoDTO> wrapper = new EntityWrapper<>();

        if(bo.getFileUserId() != null){
            wrapper.eq("file_user_id", bo.getFileUserId());
        }

        if (bo.getChildUserId() != null){
            wrapper.eq("child_user_id", bo.getChildUserId());
        }
        if(StrUtils.isNotEmpty(bo.getFileName())) {
            wrapper.like("file_name", bo.getFileName());
        }

        if(StrUtils.isNotEmpty(bo.getFileExtName())){
            wrapper.eq("file_ext_name", bo.getFileExtName());
        }

        if(bo.getType() != null){
            wrapper.eq("type", bo.getType());
        }

        if(bo.getIsMark() != null && bo.getIsMark() == 1){
            wrapper.eq("watermark", "wm");
        } else {
            wrapper.eq("child_type", 0);
        }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        wrapper.orderBy("create_time", false);
        Page<FileInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = fileInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @ApiOperation(value = "统计用户文件库使用大小",notes = "统计用户文件库使用大小")
    @PostMapping("/queryImportPurchaseExcel")
    public Result<FileInfoDTO> queryImportPurchaseExcel(Long id, Integer type) {
        // type:0询价单列表，1审核询价单
        if (id == null || type == null){
            return new Result<> (CodeEnum.FAIL_100002);
        }
        Wrapper<FileInfoDTO> wrapper = new EntityWrapper<>();
        if (type == 0){
            wrapper.eq ("create_user_id",id);
        }
        else if (type == 1){
            wrapper.eq ("child_user_id",id);
        }
        FileInfoDTO fileInfoDTO = fileInfoService.selectOne (wrapper);

        return new Result<> (fileInfoDTO);
    }
}
